import { useState, useCallback } from 'react'
import { workoutNoteService } from '@/services/workoutNoteService'

export function useWorkoutNotes() {
  const [notes, setNotes] = useState([])
  const [isLoading, setIsLoading] = useState(false)

  const loadNotes = useCallback(async () => {
    setIsLoading(true)
    try {
      const data = workoutNoteService.getNotes()
      setNotes(data)
    } catch (error) {
      console.error('加载笔记失败:', error)
    } finally {
      setIsLoading(false)
    }
  }, [])

  const addNote = useCallback(async (note) => {
    try {
      const newNote = workoutNoteService.addNote(note)
      setNotes(prev => [newNote, ...prev])
      return newNote
    } catch (error) {
      console.error('添加笔记失败:', error)
      throw error
    }
  }, [])

  const deleteNote = useCallback(async (noteId) => {
    try {
      await workoutNoteService.deleteNote(noteId)
      setNotes(prev => prev.filter(note => note.id !== noteId))
    } catch (error) {
      console.error('删除笔记失败:', error)
      throw error
    }
  }, [])

  return {
    notes,
    isLoading,
    loadNotes,
    addNote,
    deleteNote
  }
}